package com.inspur.cloud.controller.resource;

import com.inspur.cloud.entity.dataspace.common.OpEntityResult;
import com.inspur.cloud.entity.dataspace.dto.ResourceServiceDTO;
import com.inspur.cloud.entity.dataspace.vo.ResourceRetVO;
import com.inspur.cloud.entity.dataspace.vo.ResourceServiceReq;
import com.inspur.cloud.entity.dataspace.vo.StatusAllVO;
import com.inspur.cloud.entity.dataspace.vo.StatusVO;
import com.inspur.cloud.security.AuthorizationHelper;
import com.inspur.cloud.service.dataspace.StatusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import java.util.List;

@RestController
@RequestMapping("/dataspace/api/v1/resource")
@Api(value = "status",description = "资源使用情况总接口")
public class StatusController {
    @Autowired
    StatusService statusService;

    @GetMapping("/show")
    @ApiOperation(value = "各组件使用详情统计",notes = "")
    public OpEntityResult<StatusVO> showStatus(@ApiParam(value = "用户ID")
                                                   @RequestParam(value = "tenantId", required = true) Integer tenantId){
        OpEntityResult<StatusVO> result = new OpEntityResult(1,"查询成功");
        if(!AuthorizationHelper.isAdmin() && !AuthorizationHelper.getUser().getId().equals(tenantId)){
            return OpEntityResult.fail("没有权限查看");
        }
        result=statusService.showStatus(tenantId);
        return result;
    }

    @GetMapping("/all")
    @ApiOperation(value = "服务器总资源使用统计",notes = "只显示HDFS和YARN的")
    public OpEntityResult<StatusAllVO> all(){
        OpEntityResult<StatusAllVO> result = new OpEntityResult(1,"查询成功");
        result=statusService.all();
        return result;
    }

    @PostMapping("/add")
    @ApiOperation(value = "创建租户时添加所有组件")
    @RolesAllowed({"Admin"})
    public OpEntityResult<ResourceRetVO> add(
            @ApiParam(value = "给租户分配资源的对象")
            @RequestBody ResourceServiceReq resourceServiceReq){
        OpEntityResult<ResourceRetVO> result = new OpEntityResult(1,"创建资源");
        if(resourceServiceReq.getUid()==null){
            return new OpEntityResult<>(-1,"用户ID不可为空");
        }
        result=statusService.add(resourceServiceReq);
        return result;
    }

    @PutMapping("/modify")
    @ApiOperation(value = "修改租户的组件信息", notes = "只允许修改HDFS配置，并且不允许在这里删除已有的组件.Yarn的配额需要联系管理员修改")
    @RolesAllowed({"Admin"})
    public OpEntityResult<ResourceRetVO> modify(
            @ApiParam(value = "给租户分配资源的对象")
            @RequestBody ResourceServiceReq resourceServiceReq){
        OpEntityResult<ResourceRetVO> result = new OpEntityResult(1,"修改资源");
        if(resourceServiceReq.getUid()==null){
            return new OpEntityResult<>(-1,"用户ID不可为空");
        }
        result=statusService.modify(resourceServiceReq);
        return result;
    }

    @GetMapping("/one")
    @ApiOperation(value = "获取租户的所有已配置组件信息")
    @RolesAllowed({"Admin", "Tenant"})
    public OpEntityResult<List<ResourceServiceDTO>> one(
            @ApiParam(value = "租户ID")
            @RequestParam(value = "tenantId") Integer tenantId){
        OpEntityResult<List<ResourceServiceDTO>> result = new OpEntityResult(1,"查询成功");
        if(!AuthorizationHelper.isAdmin() && !AuthorizationHelper.getUser().getId().equals(tenantId)){
            return OpEntityResult.fail("没有权限查看");
        }
        result=statusService.one(tenantId);
        return result;
    }
}
